Completed
Pull Request — master (#34)
by Sander
01:06
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 1
Paths 27

Size

Total Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 27
nop 4
dl 0
loc 102
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('SettingsCtrl', ['$scope', '$rootScope', 'Settings', '$location', function ($scope, $rootScope, Settings, $location) {
0 ignored issues
show
Unused Code introduced by
The parameter $rootScope is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter $location is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter Settings is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePort: true,
44
                ignorePath: true,
45
                generatedPasswordLength: 12,
46
                remember_password: true,
47
                remember_vault_password: true,
48
                refreshTime: 60,
49
                debug: false
50
            };
51
            $scope.errors = [];
52
53
            API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) {
54
                $scope.errors = [];
55
                if (settings) {
56
                    $scope.settings = angular.copy(settings);
57
                }
58
59
                $scope.get_vaults = function () {
60
                    if (!$scope.settings.hasOwnProperty('nextcloud_host') || !$scope.settings.hasOwnProperty('nextcloud_password') || !$scope.settings.hasOwnProperty('nextcloud_username')) {
61
                        return;
62
                    }
63
                    PAPI.username = $scope.settings.nextcloud_username;
64
                    PAPI.password = $scope.settings.nextcloud_password;
65
                    PAPI.host = $scope.settings.nextcloud_host;
66
                    $scope.extension = API.runtime.getManifest().name + ' extension ' + API.runtime.getManifest().version;
67
                    PAPI.getVaults(function (vaults) {
68
                        $scope.errors = [];
69
                        var save_btn = jQuery('#save'),
70
                            login_required = jQuery('.login-req');
71
                        if (vaults.hasOwnProperty('error')) {
72
                            console.log(vaults);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
73
                            var errors = 'Invalid response from server: [' + vaults.result.status + '] ' + vaults.result.statusText;
74
                            $scope.errors.push(errors);
75
                            login_required.hide();
76
                            save_btn.hide();
77
                            $scope.$apply();
78
                            return;
79
80
                        }
81
                        login_required.show();
82
                        save_btn.show();
83
                        $scope.vaults = vaults;
84
                        $scope.$apply();
85
86
                    });
87
                };
88
89
                $scope.$watch('[settings.nextcloud_host, settings.nextcloud_username, settings.nextcloud_password]', function () {
90
                    if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
91
                        $scope.get_vaults();
92
                    }
93
                }, true);
94
95
                if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
96
                    $scope.get_vaults();
97
                }
98
                $scope.$apply();
99
            });
100
101
            $scope.saving = false;
102
            $scope.saveSettings = function () {
103
                $scope.errors = [];
104
                var settings = angular.copy($scope.settings);
105
                try {
106
                    /** global: PAPI */
107
                    PAPI.decryptString(settings.default_vault.challenge_password, settings.vault_password);
108
                } catch (e) {
109
                    $scope.errors.push('Invalid vault key!');
110
                    return;
111
                }
112
113
                $scope.saving = true;
114
                API.runtime.sendMessage(API.runtime.id, {method: "saveSettings", args: settings}).then(function () {
115
                    setTimeout(function () {
116
                        window.location = '#!/';
117
                        $scope.saving = false;
118
                    }, 750);
119
                });
120
            };
121
122
            $scope.removeSite = function (site) {
123
                var idx = $scope.settings.ignored_sites.indexOf(site);
124
                $scope.settings.ignored_sites.splice(idx, 1);
125
            };
126
127
            $scope.ignoreSite = '';
128
            $scope.addSite = function (site) {
129
                $scope.settings.ignored_sites.push(site);
130
                $scope.ignoreSite = '';
131
            };
132
133
134
            $scope.cancel = function () {
135
                window.location = '#!/';
136
            };
137
        }]);
138
}());
139
140